<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="./vue.js"></script>
    <style>
        * {
            margin: 0;
            padding: 0;
            list-style: none;
        }

        .tr {
            background-color: black;
            color: aliceblue;
            display: flex;
            justify-content: space-evenly;
            align-items: center;
        }

        .tr .item {
            flex: 1;
            background-color: black;
            color: aliceblue;
            text-align: center;
            line-height: 40px;
        }
    </style>
</head>

<body>
    <div id="app">
        {{list}}
        <div v-for="(item,index)  in list">
            值为: {{item}} 索引 {{index}}
        </div>

        {{infoList}}
        <hr>
        {{infoList[0]}} <br>{{infoList[0].id}}<br>{{infoList[0].username}}<br>{{infoList[0].userage}}
        <hr>
        {{infoList[1]}}
        <hr>
        {{infoList[2]}}
        <hr />


        <div class="table">
            <div class="tr">
                <div>姓名：<input type="text" :value="username" @input="setusername"></div>
                <div>年龄：<input type="text" :value="userage" @input="setuserage"></div>

                <div class="add" @click="addValue">新增</div>
            </div>
            <div class="tr">
                <div class="item">编号</div>
                <div class="item">用户名</div>
                <div class="item">年龄</div>
                <div class="item">操作</div>
            </div>

            <div class="tr" v-for="item  in infoList">
                <div class="item">{{item.id}}</div>
                <div class="item">{{item.username}}</div>
                <div class="item">{{item.userage}}</div>
                <div class="item" @click="delById(item.id)">删除</div>
            </div>
        </div>

    </div>

</body>
<script>

    new Vue({
        el: "#app",
        data: {
            list: [1, 2, 3, 4],

            username: "",
            userage: "",
            infoList: [{ id: 1, username: "leson", userage: 18 }, { id: 2, username: "bill", userage: 18 }, { id: 3, username: "jack", userage: 18 }]
        },

        methods: {
            delById(id) {
                this.infoList = this.infoList.filter(item => item.id != id);
                console.log(this.infoList);
            },
            addValue() {
                let id = 1;
                if (this.infoList.length) {
                    id = this.infoList.map(item => item.id).sort((a, b) => b - a)[0] + 1
                }
                if (!this.infoList.some(item => item.username == this.username)) {
                    this.infoList.push({
                        id,
                        username: this.username,
                        userage: this.userage,
                    })
                } else {
                    alert(this.username + "已经存在");
                }
            },

            setusername(e) {
                this.username = e.target.value;
            },
            setuserage(e) {
                this.userage = e.target.value;
            }
        }


    })





</script>

</html>